﻿//
//    Dialog window used to edit single item
//
define("ow-epi-listProperty/widgets/ListPropertyEditorWidget", [
"epi",
"dojo",
"dojo/_base/lang",

"dijit",
"epi/shell/widget/dialog/Dialog",

"epi/shell/widget/FormContainer"
],

function (
    epi,
    dojo,
    lang,
    dijit,
    Dialog,
    FormContainer
) {

    return dojo.declare("ow-epi-listProperty.widgets.ListPropertyEditorWidget", Dialog, {

        defaultActionsVisible: false,

        listMetadata: null,

        editItem: null,

        itemIndex: null,

        _formWidget: null,

        baseClass: 'list-property-dialog',

        postMixInProperties: function () {
            this.inherited(arguments);
            this.buttonCancel = epi.resources.action.close;
        },

        postCreate: function () {
            this.inherited(arguments);

            this.set("title", 'List item editor');
            dojo.style(this.domNode, { width: "800px", height: "" });
        },

        show: function () {
            this.inherited(arguments);

            this._ensureFormWidget();
            this._formWidget.set('value', this.editItem);
        },

        _ensureFormWidget: function () {
            if (this._formWidget) {
                return;
            }

            this._formWidget = new FormContainer({
                metadata: this.listMetadata
                // readonly support
            });

            this._formWidget.set('readOnly', this.readOnly);

            this.set("content", this._formWidget);
        },

        uninitialize: function () {
            if (this._formWidget != null) {
                this._formWidget.destroyRecursive();
            }
            this.inherited(arguments);
        },

        _setReadOnlyAttr:function(value) {
            this._set('readOnly', value);
        },

        getActions: function () {
            var buttons = [];

            if (!this.readOnly) {
                buttons.push({
                    name: this._okButtonName,
                    label: this.confirmActionText,
                    title: null,
                    settings: { "class": "Salt" },
                    action: lang.hitch(this, this._onConfirm)
                });
            }

            buttons.push({
                name: this._cancelButtonName,
                label: this.cancelActionText,
                title: null,
                action: lang.hitch(this, this._onCancel)
            });

            return buttons;
        },

        _onConfirm: function () {
            if (this._closing) {
                return;
            }
            if (!this._formWidget.validate()) {
                alert('Invalid data');
                return;
            }

            var self = this;

            this._closing = true;
            setTimeout(function () {
                self.hide();

                self.onCallback({
                    closeAction: 'insert',
                    formValues: self._formWidget.get('value'), //this._formWidget.form.value,
                    itemIndex: self.itemIndex
                });
            }, 500);
        },

        _onCancel: function () {
            this.hide();
            this.onCallback(null);
        },

        onCallback: function (value) { }
    });
});